package com.liuyingxing.javaSpringCloudAccount.controller;

import com.github.pagehelper.PageInfo;
import com.liuyingxing.entity.account.User;
import com.liuyingxing.entity.common.Result;
import com.liuyingxing.entity.common.SearchBean;
import com.liuyingxing.javaSpringCloudAccount.entity.UserVo;
import com.liuyingxing.javaSpringCloudAccount.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * @File UserController.java
 * @Auth liuxing
 * @Date 2021/8/22 10:39
 * @Email liuxing997@foxmail.com
 */
@RestController
@RequestMapping("/api/account/")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     * 127.0.0.1/api/login ---- post
     * {"userName":"admin", "password":"111111"}
     */
    @PostMapping(value = "/login", consumes = "application/json")
    public Result<User> login(@RequestBody User user) {
        return userService.login(user);
    }

    /**
     * 添加用户信息
     * 127.0.0.1/api/user ---- post
     * {"userName":"admin", "password":"111111"}
     */
    @PostMapping(value = "/user", consumes = "application/json")
    public Result<User> insertUser(@RequestBody User user) {
        return userService.insertUser(user);
    }

    /**
     * 更新用户信息
     * 127.0.0.1/api/user ---- put
     * {"id":"1", "userName":"admin", "password":"111111"}
     */
    @PutMapping(value = "/user", consumes = "application/json")
    public Result<User> updateUser(@RequestBody User user) {
        return userService.updateUser(user);
    }

    /**
     * 根据ID查询用户信息
     * 127.0.0.1/api/user/1 ---- get
     */
    @GetMapping("/user/{id}")
    public User getUserById(@PathVariable int id) {
        return userService.getUserById(id);
    }

    /**
     * 根据ID删除用户信息
     * 127.0.0.1/api/user/1 ---- delete
     */
    @DeleteMapping("/user/{id}")
    public Result<Object> deleteUserById(@PathVariable int id) {
        return userService.deleteUserById(id);
    }

    /**
     * 分页查询用户信息
     * 127.0.0.1/api/users ---- post
     * {"currentPage":"1","pageSize":"5","orderBy":"id","direction":"desc","keyWord":""}
     */
    @PostMapping(value = "/users", consumes = "application/json")
    public PageInfo<User> getUsersBySearchBean(@RequestBody SearchBean searchBean) {
        return userService.getUsersBySearchBean(searchBean);
    }

    /**
     * 根据关键字获取用户信息
     * 127.0.0.1/api/users/excel?keyWord=hj ---- get
     */
    @GetMapping(value = "/users/excel")
    public void exportExcel(@RequestParam String keyWord,
                            HttpServletResponse response) throws IOException {
        List<User> users = userService.getUsersByKeyWord(keyWord);
    }

    /**
     * 根据ID获取用户信息（包括城市信息）
     * 127.0.0.1:8762/api/account/userVo/1 ---- get
     */
    @GetMapping("/userVo/{id}")
    public UserVo getUserVoById(@PathVariable int id) {
        return userService.getUserVoById(id);
    }
}
